package com.tanhua.server.controller;

import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tanhua")
@Slf4j
public class TodayBestController {

    @Autowired
    private TodayBestService todayBestService;

    /**
     * 查询今日佳人
     *
     * @param token
     * @return
     */
    @GetMapping("todayBest")
    public ResponseEntity<TodayBest> queryTodayBest(@RequestHeader("Authorization") String token) {
        try {
            //1.通过todayBestService业务层调用查询今日佳人queryTodayBest(token)方法,传入token值,返回的是todayBest对象
            TodayBest todayBest = this.todayBestService.queryTodayBest(token);

            //2.如果返回的对象不是空
            if (null != todayBest) {
                //2.1 则响应ok(),并响应返回的todayBest对象
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            //3.若上述代码出现异常,则响应接收到的token值,以及错误信息
            log.error("查询今日佳人出错~ token = " + token, e);
        }

        //4.若返回的对象为空,则响应错误状态码
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 推荐用户
     * @param token
     * @param param
     * @return
     */
    @GetMapping("recommendation")
    public ResponseEntity<PageResult> queryTodayBest(@RequestHeader("Authorization") String token, RecommendUserQueryParam param) {
        try {
            PageResult pageResult = this.todayBestService.queryRecommendation(token,param);

            if(null != pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {

            log.error("查询推荐列表失败~ token" + token , e);
        }

        return null;
    }

    /**
     * 查询今日佳人详细信息
     * @param userId
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity<TodayBest> queryTodayBest(@PathVariable("id") Long userId){
        try {
            TodayBest todayBest = this.todayBestService.queryTodayBest(userId);

            return ResponseEntity.ok(todayBest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询陌生人问题
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> queryQuestion(@RequestParam("userId") Long userId){
        try {
            String question = this.todayBestService.queryQuestion(userId);

            return ResponseEntity.ok(question);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 回复陌生人问题
     * @param param
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replyQuestion(@RequestBody Map<String, Object> param){
        try {
            Long userId = Long.valueOf(param.get("userId").toString());
            String reply = param.get("reply").toString();

            Boolean result = this.todayBestService.replyQuestion(userId,reply);

            if (result){
                return ResponseEntity.ok(null);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜附近
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender",required = false)String gender,
                                                          @RequestParam(value = "distance",defaultValue = "2000")String distance){
        try {
            List<NearUserVo> list = this.todayBestService.queryNearUser(gender,distance);

            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 探花卡片功能
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardsList(){
        try {
            List<TodayBest> list = this.todayBestService.queryCardsList();

            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 喜欢
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> likeUser(@PathVariable("id") Long likeUserId){
        try {
            this.todayBestService.likeUser(likeUserId);

            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> disLikeUser(@PathVariable("id") Long likeUserId){
        try {
            this.todayBestService.disLikeUser(likeUserId);

            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
